$(function (){

	var isapp=getQueryString('isapp');
    if(isapp=="1"){
        token = getQueryString("token");
        drawNum(token);
    }else{
    	if(token){
    		drawNum(token);
    	}
    }
    var unbind=true;
	var rotateTimeOut = function (){
		$('#rotate').rotate({
			angle:0,
			animateTo:2160,
			duration:8000,
			callback:function (){
				alert('网络超时，请检查您的网络设置！');
			}
		});
	};
	var bRotate = false;

	var rotateFn = function (awards, angles, txt){
		bRotate = !bRotate;
		$('#rotate').stopRotate();
		$('#rotate').rotate({
			angle:0,
			animateTo:angles+2160,
			duration:10000,
			callback:function (){
				console.log(txt);
				var imgurl="";
				if(awards < 5){
					imgurl="src/img/red.png";
				}else if(awards > 4 && awards < 8){
					imgurl="src/img/luckdraw_jinbi.png";
				}else if(awards == "8"){
					mgurl="src/img/rechrage_100.png";
				}else if(awards == "9"){
					mgurl="src/img/luckdraw_lian.png";
				}
				$(".luckdraw_result_content .top .luckdraw_result_gift img").attr("src",imgurl)
				$(".luckdraw_result_content .top .luckdraw_textbig").html("["+txt+"]")
				$(".luckdraw_result").removeClass("none")
				unbind=true;
				bRotate = !bRotate;
			}
		})
	};

	$('.pointer').click(function (){
		if(!token){
			$(".complaint_alertmsg .msg").html("请先登录")
            $(".complaint_alertmsg").removeClass("none");
             setTimeout(function(){
                $(".complaint_alertmsg").addClass("none");
            },2000)
			return;
		}
		if(bRotate)return;
		if(unbind){
			$.ajax({
		        type:"post",
		        url:api_url+'/OpenAPI/v1/Luckdraw/luckDraw',
		        jsonp:'callback',
		        dataType:'jsonp',
		        data:{token:token},
		        success:function(data){
		        	console.log(data)
		        	if(data.code=="0"){
		                var item =data.data.num;
			        	switch (item) {
							case '1':
								//var angle = [88, 137, 185, 235, 287];
								rotateFn(1, 360, data.data.title);
								break;
							case '2':
								//var angle = [137, 185, 235, 287];
								rotateFn(2, 320, data.data.title);
								break;
							case '3':
								//var angle = [137, 185, 235, 287];
								rotateFn(3, 280, data.data.title);
								break;
							case '4':
								//var angle = [185, 235, 287];
								rotateFn(4, 240, data.data.title);
								break;
							case '5':
								//var angle = [185, 235, 287];
								rotateFn(5, 200, data.data.title);
								break;
							case '6':
								//var angle = [235, 287];
								rotateFn(6, 160, data.data.title);
								break;
							case '7':
								//var angle = [287];
								rotateFn(7, 120, data.data.title);
								break;
							case '8':
								//var angle = [287];
								rotateFn(8, 80, data.data.title);
								break;
							case '9':
								//var angle = [287];
								rotateFn(9, 40, data.data.title);
								break;
						}
						drawNum(token);
		            }else{
		                $(".complaint_alertmsg .msg").html(data.msg)
		                $(".complaint_alertmsg").removeClass("none");
		                 setTimeout(function(){
			                $(".complaint_alertmsg").addClass("none");
			            },2000)
		            }
		        }
		    })
		}
		unbind=false;
		// var item = rnd(1,9);
		// console.log(item);
	});
});
function rnd(n, m){
	return Math.floor(Math.random()*(m-n+1)+n)
}

function drawNum(tokeni){
    $.ajax({
        type:"post",
        url:api_url+'/OpenAPI/v1/Luckdraw/drawNum',
        jsonp:'callback',
        dataType:'jsonp',
        data:{token:tokeni},
        success:function(data){
            console.log(data);
            if(data.code=="0"){
            	$(".luckdraw_label span").html(data.data+"次");
            }else{
            	$(".luckdraw_label span").html("0次");
            }
            
        }
    })
}
function drawdetail(){
    $.ajax({
        type:"post",
        url:api_url+'/OpenAPI/v1/Luckdraw/drawdetail',
        jsonp:'callback',
        dataType:'jsonp',
        data:{token:token},
        success:function(data){
            console.log(data);
            var Li='';
            var list=data.data;
            if(list.length>0){
            	for(var i=0;i<list.length;i++){
	            	Li+='<li><span class="con">'+list[i]['draw_title']+'</span><span class="time">'+list[i]['create_at']+'</span></li>';
	            }
            }else{
            	Li+='<li style="text-align:center;font-size:16px;color:#666;padding-top:50px;">暂无奖品</li>'
            }
            $("#drawdetail").html(Li);
        }
    })
}



drawLimit();
function drawLimit(){
	$.ajax({
        type:"post",
        url:api_url+'/OpenAPI/v1/Luckdraw/drawLimit',
        jsonp:'callback',
        dataType:'jsonp',
        data:{token:token},
        success:function(data){
            console.log(data);
            var Li='';
            var list=data.data;
            if(list.length>0){
            	for(var i=0;i<list.length;i++){
	            	Li+='<li>'+list[i]['nickname']+'<span>成功获得了'+list[i]['draw_title']+'</span></li>';
	            }
            }else{
            	for(var i=0;i<10;i++){
	            	Li+='<li>没有记录</li>';
	            }
            }
            $(".luckdraw_footer #scroll").html(Li);
        }
    })
}
